﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace AppEszköz.Trace
{
    public class TextTraceListener : TextWriterTraceListener
    {
        public TextTraceListener(Stream stream, string name)
          : base(stream, name)
        {
        }

        public TextTraceListener(Stream stream)
          : base(stream)
        {
        }

        public TextTraceListener(string fileName, string name)
          : base(fileName, name)
        {
        }

        public TextTraceListener(string fileName)
          : base(fileName)
        {
        }

        public TextTraceListener(TextWriter writer, string name)
          : base(writer, name)
        {
        }

        public TextTraceListener(TextWriter writer)
          : base(writer)
        {
        }

        public override void Write(string message)
        {
            base.Write(this.getPreambleMessage() + message);
        }

        public override void Write(string message, string category)
        {
            base.Write(this.getPreambleMessage() + "[" + category + "]" + message);
        }

        public override void WriteLine(string message)
        {
            base.WriteLine(this.getPreambleMessage() + message);
        }

        public override void WriteLine(string message, string category)
        {
            base.WriteLine(this.getPreambleMessage() + "[" + category + "]" + message);
        }

        private string getPreambleMessage()
        {
            StringBuilder stringBuilder = new StringBuilder();
            StackTrace stackTrace = new StackTrace();
            int index1 = 0;
            var reflectedType = MethodBase.GetCurrentMethod().ReflectedType;
            if (reflectedType != null)
            {
                string str = reflectedType.ToString();
                MethodBase method;
                string fullName = null;
                do
                {
                    ++index1;
                    method = stackTrace.GetFrame(index1).GetMethod();
                    if (method.ReflectedType != null) fullName = method.ReflectedType.FullName;
                }
                while (fullName != null && (fullName.StartsWith("System") || fullName.EndsWith(str) || fullName.StartsWith("FirstMerit.AppTools.Trace.RollingTextTraceListener")));
                stringBuilder.Append(DateTime.Now.ToString());
                stringBuilder.Append(": ");
                stringBuilder.Append(fullName);
                if (!method.Name.StartsWith("."))
                    stringBuilder.Append(".");
                stringBuilder.Append(method.Name);
                stringBuilder.Append("(");
                ParameterInfo[] parameters = method.GetParameters();
                for (int index2 = 0; index2 < parameters.Length; ++index2)
                {
                    stringBuilder.Append(parameters[index2].ParameterType.Name);
                    stringBuilder.Append(" ");
                    stringBuilder.Append(parameters[index2].Name);
                    if (index2 + 1 != parameters.Length)
                        stringBuilder.Append(", ");
                }
            }
            stringBuilder.Append("): ");
            return stringBuilder.ToString();
        }
    }
}